package pl.perseus.action;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;

import org.springframework.beans.factory.annotation.Autowired;

import pl.perseus.model.Classes;
import pl.perseus.model.Degree;
import pl.perseus.model.Student;
import pl.perseus.model.User;
import pl.perseus.service.classes.IClassesService;
import pl.perseus.service.degree.IDegreeService;
import pl.perseus.service.student.IStudentService;
import pl.perseus.service.user.IUserService;

public class GetDegreeReportAcion extends BaseAction {
	
	@Autowired
	IClassesService classesService;
	
	@Autowired
	IStudentService studentService;
	
	@Autowired
	IDegreeService degreeService;
	
	@Autowired
	IUserService userService;
	
	@Getter
	@Setter
	Student studentFilter;
	
	@Getter
	@Setter
	Classes classesFilter;
	
	@Getter
	@Setter
	Map<String, Object> result;
	
	@Override
	public String execute() {
		result = new HashMap<String, Object>();
		List<Map<String, Object>> items = new LinkedList<Map<String, Object>>();
		DecimalFormat doubleFormatter = new DecimalFormat(".##");
		
		List<Classes> classes = getClassesForUser();
		for(Classes c : classes){
			Map<String, Object> classesMap = new HashMap<String, Object>();
			classesMap.put("id", c.getId().toString());
			classesMap.put("label", c.getClassname()+"  "+c.getClassroom()+"  "+c.getSDate()+"  "+c.getSHour() + "  " + constructTeachersString(c));
			classesMap.put("fun", "");
			classesMap.put("avg", "");
			List<Student> studentsInClasses = getStudentInClasses(c);
			List<Map<String, Object>> studentList = new LinkedList<Map<String, Object>>();
			for(Student s : studentsInClasses){
				Map<String, Object> studentMap = new HashMap<String, Object>();
				studentMap.put("id", c.getId().toString()+s.getId().toString());
				studentMap.put("label", s.getFirstname()+" "+s.getSurname());
				studentMap.put("fun", "avg:");
				Double avgD = degreeService.getAVGForStudentInClasses(s, c);
				String avgS = avgD == null ? "" : doubleFormatter.format(avgD);
				studentMap.put("avg", avgS);
				List<Degree> degrees = degreeService.getDegreesForStudentAndClasses(s, c);
				List<Map<String, Object>> degreeList = new LinkedList<Map<String, Object>>();
				for(Degree d : degrees){
					Map<String, Object> degreeMap = new HashMap<String, Object>();
					degreeMap.put("id", c.getId().toString()+s.getId().toString()+d.getId().toString());
					degreeMap.put("type", d.getDegreename());
					degreeMap.put("grade", d.getDegreevalue());
					degreeMap.put("desc", d.getDescription());
					degreeList.add(degreeMap);
				}
				studentMap.put("grades", degreeList);
				studentList.add(studentMap);
			}
			classesMap.put("students", studentList);
			items.add(classesMap);
		}
		
		result.put("label", "label");
		result.put("identifier", "id");
		result.put("items", items);
		
		return SUCCESS;
	}
	
	private List<Student> getStudentInClasses(Classes c){
		List<Student> studentsInClasses = studentService.getStudentsInClasses(c);
		if(studentFilter != null && studentFilter.getId() != null){
			for(Iterator<Student> it = studentsInClasses.iterator(); it.hasNext(); ){
				Student s = it.next();
				if(!s.getId().equals(studentFilter.getId()))
					it.remove();
			}
		}
			
		return studentsInClasses;
	}
	
	private List<Classes> getClassesForUser(){
		List<Classes> classes = classesService.getClassesForUser(getCurrentUserLogin());
		if(classesFilter != null && classesFilter.getId() != null){
			for(Iterator<Classes> it = classes.iterator(); it.hasNext(); ){
				Classes c = it.next();
				if(!c.getId().equals(classesFilter.getId()))
					it.remove();
			}
		}
			
		return classes;
	}
	
	private String constructTeachersString(Classes c){
		String list ="";
		
		for(User u : userService.getTeachersForClasses(c)){
			list += u.getUsername();
		}
		
		 return list;
	}

}
